package com.xyf.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.seventh.common.pojo.R;
import com.xyf.mapper.TbMaintainRecordMapper;
import com.xyf.pojo.entity.TbMaintainRecord;
import com.xyf.pojo.vo.MaintainRecordVo;
import com.xyf.service.TbMaintainRecordService;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;

/**
 * @program: clms-seventh
 * @description: 实现类
 * @author: 徐亚飞
 * @create: 2021-07-23 20:00
 **/
@Service
@ApiModel(value = "维修记录实现类" , description = "维修记录实现类")
public class TbMaintainRecordServiceImpl implements TbMaintainRecordService {
    
    @Resource
    TbMaintainRecordMapper mapper;

    @Override
    @ApiModelProperty("分页查询维修记录")
    public R selectRecordByPage( Integer currentPage, Integer pageSize, MaintainRecordVo maintainRecordVo) {
        System.out.println("111111111111111");

//        String startDate1=maintainRecordVo.getStartDate();
//        String endtDate1=maintainRecordVo.getEndDate();
//        String newStarDate=startDate1.substring(0,10);
//        String endStarDate=endtDate1.substring(0,10);
//        System.out.println(newStarDate);
//        System.out.println(endStarDate);
        System.out.println(maintainRecordVo);
        Page<TbMaintainRecord> page = new Page<>(currentPage,pageSize);
        QueryWrapper<TbMaintainRecord> wrapper=new QueryWrapper<>();



        if (StringUtils.isNotEmpty(maintainRecordVo.getCarName())){
            wrapper.like("carName",maintainRecordVo.getCarName());
        }
        if (StringUtils.isNotEmpty(maintainRecordVo.getName())){
            wrapper.like("name",maintainRecordVo.getName());
        }




        if (StringUtils.isNotEmpty(maintainRecordVo.getStartDate())){
            wrapper.ge("r.addtime",maintainRecordVo.getStartDate());
        }
        if(StringUtils.isNotEmpty(maintainRecordVo.getEndDate())){
            wrapper.le("r.addtime",maintainRecordVo.getEndDate());
        }

        wrapper.orderByDesc("r.addtime");



        IPage<TbMaintainRecord> page1=mapper.selectRecordByPage(page,wrapper);
        return R.ok().put("records",page1.getRecords()).put("total",page1.getTotal());
    }

    @Override
    @ApiModelProperty("根据Id查询维修记录")
    public TbMaintainRecord selectById(Integer id) {
        TbMaintainRecord tbMaintainRecord = mapper.selectById(id);
        return tbMaintainRecord;
    }

    @Override
    @ApiModelProperty("添加维修记录")
    public R insertMaintainRecord(TbMaintainRecord tbMaintainRecord) {
        int i = mapper.insert(tbMaintainRecord);
        if (i>0){
            return R.ok();
        }else {
            return R.error();
        }
    }

    @Override
    @ApiModelProperty("删除维修记录")
    public R deleteMaintainRecord(Integer id) {
        int i = mapper.deleteById(id);
        if (i>0){
            return R.ok();
        }else {
            return R.error();
        }
    }

    @Override
    @ApiModelProperty("修改维修记录")
    public R updateMaintainRecord(TbMaintainRecord tbMaintainRecord) {
        int i = mapper.updateById(tbMaintainRecord);
        if (i>0){
            return R.ok();
        }else {
            return R.error();
        }
    }
}
